var _store;
var _sort;
Ext.onReady(function(){
		var _columnModle = new Ext.grid.ColumnModel({
	 		columns:[
	 		{
	 			header: "环节", menuDisabled: false,dataIndex:'description'
	 		},
	 		{
	 			header: "姓名", menuDisabled: false,dataIndex:'userName'
 			},
	 		{
	 			header: "个人常用语", menuDisabled: false,dataIndex:'sentence'
 			},
	 		{
	 			header: "操作", menuDisabled: false,dataIndex:'handler',renderer:function(s,m,rec){
	 				var str = "<a href=\"javaScript:updateperson('"+rec.data.id +"','"+rec.data.sentence +"') \">编辑</a>"+
	 				"&nbsp;&nbsp;<a href=\"javaScript:deletePerson('"+rec.data.id +"') \">删除</a>";
	 				return str;
	 			}
	 		}
	 		]
	 	});
	 	
					
		_store = new Ext.data.JsonStore({
	 		fields:['description','id','mainId','userName','sentence','handler'],
	 		url: Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/changyong.do?method=getGrChangYongyu',
	 		root: 'root',
            totalProperty: 'totalProperty'
	 	});
	 

		 	
	var bbar = new Ext.PagingToolbar({
		pageSize:10,
		store:_store,

		   //显示右下角信息		   
		   displayInfo:true,
		   displayMsg:'当前记录 {0} -- {1} 条 共 {2} 条记录',
	       prevText:"上一页",
		   nextText:"下一页",
		   refreshText:"刷新",
		   lastText:"最后页",
		   firstText:"第一页",
		   beforePageText:"当前页",
		   afterPageText:"共{0}页",
		   emptyMsg:'没有记录',
		   items:[
                '-','每页显示行数:',
                 {  name:'txtpagesize',
                	id:'txtpagesize',
                	xtype:'numberfield',
					emptyText:10,
					width:30          
                },{
                	text: '确定',
			            handler: function(){
			                bbar.pageSize = parseInt(Ext.getCmp('txtpagesize').getValue());
					        _store.reload({params:{start:0,limit:bbar.pageSize}}); 
			                }
            }]
	});
	
	 	var _toolbar;
	 	
	 	 _toolbar = new Ext.Toolbar({
	         items: [
	           {
	               id: 'buttonA',
				   iconCls: 'add',
	               text: "新建",
	               handler: function() {
	        	   var	_huanjie = new Ext.data.JsonStore({
				 		fields:['description','nodeId'],
				 		url: Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/changyong.do?method=getCYYhuanjie',
				 		root: 'huangjie'
				 	});
	        	   _huanjie.reload();
				   var simple = new Ext.FormPanel({
			            baseCls: 'x-plain',
					    defaultType: 'textfield',
					    bodyStyle:"padding:10px 10px 10px 15px",
				        items: [{
				        	    id:'changyongyu',
				                fieldLabel: '常用语',
				                name: 'changyongyu',
				                allowBlank:false,
				                width:190
				            },new Ext.form.ComboBox({
				                        fieldLabel: '环节',
				                        hiddenName:'nodeId',
				                        store: _huanjie,
				                        valueField:'nodeId',
				                        displayField:'description',
				                        typeAhead: true,
				                        mode: 'local',
				                        editable: false ,
				                        triggerAction: 'all',
				                        emptyText:'请选择环节',
				                        selectOnFocus:true,
				                        width:190
				                    })
				        ]
				    });

					
					    var window = new Ext.Window({
					        title: '默认常用语新增',
					        width: 350,
					        height:150,
					        minWidth: 350,
					        minHeight: 150,
					        layout: 'absolute',
					        plain:true,
					        bodyStyle:'padding:5px',
					        buttonAlign:'center',
					        items: simple,
					        modal:true,
					        buttons: [{
					            text: '确定',
					            handler:function(){
					        	            var changyongyu = Ext.get("changyongyu").getValue();
					        	            var node = Ext.get("nodeId").getValue();
					        	            if(changyongyu.length>20){
					        	            	Ext.MessageBox.show({ title:Ext.systemTitle,
													   msg: '常用语不能多于20字！',
													   buttons: Ext.Msg.OK,						  					
													   icon: Ext.MessageBox.INFO
													});
					        	            }else if(changyongyu == ""){
					        	            	Ext.MessageBox.show({ title:Ext.systemTitle,
													   msg: '常用语不能为空！',
													   buttons: Ext.Msg.OK,						  					
													   icon: Ext.MessageBox.INFO
													});
					        	            }else if(node == ""){
					        	            	Ext.MessageBox.show({ title:Ext.systemTitle,
													   msg: '环节不能为空！',
													   buttons: Ext.Msg.OK,						  					
													   icon: Ext.MessageBox.INFO
													});
					        	            }else{
												Ext.Ajax.request({
													url:Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/changyong.do?method=addGRCYY',
												    params: {nodeId:Ext.get("nodeId").getValue(),changyongyu:Ext.get("changyongyu").getValue()},
												    success:function(){
												    	Ext.MessageBox.show({ title:Ext.systemTitle,
														   msg: '添加成功！',
														   buttons: Ext.Msg.OK,						  					
														   icon: Ext.MessageBox.INFO
														});
												    	 _store.reload();
												    },
												    failure:function(){
												    	Ext.MessageBox.show({ title:Ext.systemTitle,
														   msg: '添加失败！',
														   buttons: Ext.Msg.OK,						  					
														   icon: Ext.MessageBox.INFO
														});
												    }
												});	
												window.close();
					            }}
					        },{
					            text: '取消',
					            handler:function(){window.close();}
					        }]
					    });
					
					    window.show();
	               }
				}]
			});
	

		var _table = new Ext.grid.GridPanel({
			colModel:_columnModle,
			sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
	 		store:_store,
		    width: Ext.get("showRegional").getWidth(),
		    height:Ext.get("showRegional").getHeight(),
		    title: _title,
		    renderTo:Ext.get('showRegional'),
		    frame: true,
		    viewConfig:{
		    	forceFit: true
		    },
		    clicksToEdit:1, 
		    layout : 'fit',// 表格内列内容填充满,按列头比例填充 
		    loadMask:{ 
			msg : '数据加载中，请稍后...' 
			}, 
		    stripeRows:true,
		    tbar: _toolbar,
		    bbar:bbar


		});
		_store.load({params:{start:0, limit:10}});	
});

/**
 * 删除
 */
function deletePerson(id,mainId){
	Ext.MessageBox.confirm(Ext.systemTitle, '是否删除此常用语?', function(btn){
		if(btn=='yes'){
			Ext.Ajax.request({
				url:Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/changyong.do?method=deletePersonal',
			    params: { id: id},
			    success:function(){
			    	Ext.MessageBox.show({ title:Ext.systemTitle,
					   msg: '删除成功！',
					   buttons: Ext.Msg.OK,						  					
					   icon: Ext.MessageBox.INFO
					});
			    	 _store.reload();
			    },
			    failure:function(){
			    	Ext.MessageBox.show({ title:Ext.systemTitle,
					   msg: '删除失败！',
					   buttons: Ext.Msg.OK,						  					
					   icon: Ext.MessageBox.INFO
					});
			    }
			});	
		}
	});
}
function updateperson(id,sentence){
	Ext.MessageBox.prompt("个人常用语修改","请输入常用语",function(bnt,text){
		if(bnt=="ok"){
			if(text.length>20){
				Ext.MessageBox.show({ title:Ext.systemTitle,
					   msg: '常用语字数不能大于20！',
					   buttons: Ext.Msg.OK,						  					
					   icon: Ext.MessageBox.INFO
					});
			}else{
			Ext.Ajax.request({
				url:Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/changyong.do?method=updatePerson',
			    params: { id: id,changyongyu:text},
			    success:function(){
			    	Ext.MessageBox.show({ title:Ext.systemTitle,
					   msg: '更新成功！',
					   buttons: Ext.Msg.OK,						  					
					   icon: Ext.MessageBox.INFO
					});
			    	 _store.reload();
			    },
			    failure:function(){
			    	Ext.MessageBox.show({ title:Ext.systemTitle,
					   msg: '更新失败！',
					   buttons: Ext.Msg.OK,						  					
					   icon: Ext.MessageBox.INFO
					});
			    }
			});	}
		}
	},this,false,sentence);
}

 function windowonload(){
         if (_sessionUser==null){
    	    Ext.MessageBox.alert('提示', '请先登录',clickLink);     
         }
        }
        
function clickLink(){
		window.location.href='';
	}		
		